package com.atguigu.daijia.driver.service.impl;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.DriverStatusEnum;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.driver.client.DriverInfoFeignClient;
import com.atguigu.daijia.driver.service.LocationService;
import com.atguigu.daijia.map.client.LocationFeignClient;
import com.atguigu.daijia.model.entity.driver.DriverSet;
import com.atguigu.daijia.model.form.map.UpdateDriverLocationForm;
import com.atguigu.daijia.model.form.map.UpdateOrderLocationForm;
import com.atguigu.daijia.model.vo.driver.DriverSetVo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class LocationServiceImpl implements LocationService {
    @Resource
    private LocationFeignClient locationFeignClient;
    @Resource
    private DriverInfoFeignClient driverInfoFeignClient;

    /*
     * @param updateDriverLocationForm
     * @==============History===============<br/>
     * @Description //   开启接单服务:更新司机经纬度位置
     * @Date 2024-8-24 23:32 
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return java.lang.Boolean
     */
    @Override
    public Boolean updateDriverLocation(UpdateDriverLocationForm locationForm) {
        // 获取司机的司机设置信息,司机开始接单在更新司机的位置信息
        Long driverId = locationForm.getDriverId();
        Result<DriverSetVo> driverSetResult = driverInfoFeignClient.getDriverSet(driverId);
        Integer code = driverSetResult.getCode();
        if(Objects.equals(code, ResultCodeEnum.SUCCESS.getCode()))
        {
            DriverSetVo driverSetVo = driverSetResult.getData();
            Integer status = driverSetVo.getServiceStatus();
            if(Objects.equals(status, DriverStatusEnum.START_SERVER_STATUS.getCode()))
            {
                Result<Boolean> locationResult = locationFeignClient.updateDriverLocation(locationForm);
                return locationResult.getData();
            }
        }
        throw new GuiguException(ResultCodeEnum.NO_START_SERVICE);
    }

    /*
     * @param updateOrderLocationForm
     * @==============History===============<br/>
     * @Description //   司机赶往代驾起始点:更新订单位置到Redis缓存
     * @Date 2024-9-10 22:37
     * @Author < a href="15991488967@163.com">chen</ a>
     * @return java.lang.Boolean
     */
    @Override
    public Boolean updateOrderLocationToCache(UpdateOrderLocationForm updateOrderLocationForm)
    {
        Result<Boolean> result = locationFeignClient.updateOrderLocationToCache(updateOrderLocationForm);
        if(Objects.equals(result.getCode(), ResultCodeEnum.SUCCESS.getCode()))
        {
            return result.getData();
        }
        throw new GuiguException(ResultCodeEnum.FAIL);
    }
}
